#include "oeasykey.h"
#include <QFileInfo>

OeasyKey::OeasyKey()
{

}

QString OeasyKey::generaKey(int facenum, int mutinum, int carnum)
{
    QSettings *settings = new QSettings(OeasyKeyPath, QSettings::IniFormat);
    QString magic = "OEASY";
    QString mac = getHostMacAddress();

    QString value = QString::number(facenum) + magic + QString::number(mutinum)+ mac + QString::number(carnum);
    QByteArray md5 = QCryptographicHash::hash(value.toLatin1(), QCryptographicHash::Md5);
    //qDebug()<<"zzz == "<<md5;
    QString ret(md5.toHex().toUpper());
    int len = ret.length()/8;
    for(int i=1;i< len;i++)
    {
        ret.insert(8*i+i-1,"-");
    }
    ret.append("-");
    QString face =  QString("%1").arg(facenum, 2, 16,QLatin1Char('0'));//k为int型或char型都可
    ret.append(face);
    QString muti =  QString("%1").arg(mutinum, 2, 16,QLatin1Char('0'));//k为int型或char型都可
    ret.append(muti);
    QString car =  QString("%1").arg(carnum, 2, 16,QLatin1Char('0'));//k为int型或char型都可
    ret.append(car);

    settings->setValue("CurIPCCount", 0);
    settings->beginGroup("KeyList");
    settings->setValue("1", ret);
    settings->sync();
    settings->endGroup();
    delete settings;
    settings = Q_NULLPTR;
    return ret;
}


QByteArray OeasyKey::parseKey(QString keyString, int &face, int &muti, int &car)
{

    face = 0;
    muti = 0;
    car = 0;
    if(keyString.size() != 42) return "";
    bool ok;
    QByteArray ret;
    keyString = keyString.trimmed();
    keyString = keyString.simplified();
    QStringList sl = keyString.split("-");
    QString countString= sl.last();
    sl.removeLast();
    QString allkey;
    foreach (QString s, sl) {
        allkey += s;
    }
    int length = allkey.length()/2;
    for(int i = 0; i < length; i++)
    {
        QString temp;
        temp.append(allkey.at(2*i));
        temp.append(allkey.at(2*i + 1));
        if(!temp.isEmpty())
        {
            char c = temp.toInt(&ok,16)&0xFF;
            if(ok){
                ret.append(c);
            }
        }
    }
    //parse count
    for(int i =0 ;i < countString.length()/2; i++)
    {
        QString temp;
        temp.append(countString.at(2*i));
        temp.append(countString.at(2*i + 1));
        if(!temp.isEmpty())
        {
            int num = temp.toInt(&ok,10);
            if(ok){
                if(i == 0){ face = num;}
                if(i == 1){ muti = num;}
                if(i == 2){ car = num;}
            }
        }
    }
    return ret;
}

QString OeasyKey::getHostMacAddress()
{
    QList<QNetworkInterface> nets = QNetworkInterface::allInterfaces();// 获取所有网络接口列表
    int nCnt = nets.count();
    QString strMacAddr = "";
    for(int i = 0; i < nCnt; i ++)
    {
        // 如果此网络接口被激活并且正在运行并且不是回环地址，则就是我们需要找的Mac地址
        if(nets[i].flags().testFlag(QNetworkInterface::IsUp) && nets[i].flags().testFlag(QNetworkInterface::IsRunning) && !nets[i].flags().testFlag(QNetworkInterface::IsLoopBack))
        {
            strMacAddr = nets[i].hardwareAddress();
            break;
        }
    }
    return strMacAddr;
}

bool OeasyKey::checkKey(QString keyString)
{
    QFileInfo file(OeasyKeyPath);
    bool e = file.exists();
    QSettings *settings = new QSettings(OeasyKeyPath, QSettings::IniFormat);
    bool b = settings->isWritable();
    settings->beginGroup("KeyList");
    QStringList allkey = settings->allKeys();
    bool ret = false;
    for(int i =0 ;i < allkey.size() && i < 20; i++)
    {
        QString value = settings->value(allkey.at(i)).toString();
        if(value == keyString)
        {
           ret = true;
           break;
        }
    }
    settings->endGroup();
    delete settings;
    settings = Q_NULLPTR;
    return ret;
}
